/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package functions;

import Interfaces.Loadable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingWorker;

/**
 *
 * @author JArik
 */
public class HTTPRequest
{
    protected String url;

    protected SwingWorker worker;
    protected String data;
    protected Loadable handler;
    protected String response;

    public HTTPRequest()
    {

    }
    
    public HTTPRequest(String url)
    {
        this.url = url;
        response ="";
    }
    public void setVars(HashMap vars)
    {
        data = "";
        Set entries = vars.keySet();
        Iterator it = entries.iterator();
        while(it.hasNext())
        {
            Object key = it.next();
            Object value = vars.get(key);
            if(!data.isEmpty())
                data +="&";
            try {
                data += URLEncoder.encode(key + "", "UTF-8") + "=" + URLEncoder.encode(value + "", "UTF-8");
            } catch (UnsupportedEncodingException ex) {
                data += key + "=" + value;
            }
        }
    }
    public String getResponse() {
        return response;
    }
    public void sendRequest(final Loadable obj) throws MalformedURLException, IOException
    {
        handler = obj;
        final HTTPRequest thisReq = this;
        worker = new SwingWorker()
        {
            @Override
            protected Object doInBackground() throws Exception
            {
                obj.load();
                URL urlReq = new URL(url);
                System.out.println(data);
                URLConnection reqConnection = urlReq.openConnection();
                handler.progress(10);
                reqConnection.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(reqConnection.getOutputStream());
                wr.write(data);
                wr.flush();
                handler.progress(20);
                BufferedReader in = new BufferedReader(
                                        new InputStreamReader(
                                        reqConnection.getInputStream(),"UTF-8"));
                handler.progress(25);
                String inputLine;
                int cLength = reqConnection.getContentLength();
                while ((inputLine = in.readLine()) != null)
                {
                    response += inputLine;
                    handler.progress(25 + (int)(75*((double)response.length()*100/(double)cLength)));
                }
                wr.close();
                in.close();
                handler.progress(100);
                return response;
            }
            protected void done()
            {
                obj.done(thisReq);
            }
        };
        worker.execute();
    }

}
